import { _decorator, AudioClip, AudioSource, Color, Component, EventTouch, find, Node, Sprite, UITransform, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
import { Blueprint } from './Blueprint';

@ccclass('Toolbox')
export class Toolbox extends Component {
    @property({type: Node})
    toolContentNode: Node = null!

    @property({type: AudioClip})
    selectAudio: AudioClip = null!

    selectedTool: string = ''
    audioSource: AudioSource = null!

    start() {
        this.setDeafaultTool()
        this.initComponents()
        this.initTouchEvents()
    }

    /** 
     * @zh
     * 设置默认工具。
    */
    setDeafaultTool() {
        this.selectedTool = 'pencil'
        this.toolContentNode.children[0].getComponent(Sprite).color = new Color().fromHEX('#ffffff')
    }

    /** 
     * @zh
     * 初始化组件。
    */
    initComponents() {
        this.audioSource = find('Canvas').getComponent(AudioSource)
    }

    /** 
     * @zh
     * 初始化触摸事件。
    */
    initTouchEvents() {
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
    }

    /** 
     * @zh
     * 触摸结束时执行的逻辑。
    */
    onTouchEnd(event: EventTouch) {
        let loc = event.getUILocation()
        let pos = this.toolContentNode.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(loc.x, loc.y, 0))

        for (let i=0; i<this.toolContentNode.children.length; i++) {
            let toolItem = this.toolContentNode.children[i]
            if (toolItem.getComponent(UITransform).getBoundingBox().contains(new Vec2(pos.x, pos.y))) {
                // 添加到撤销操作中
                find('Canvas/Blueprint Background/Blueprint').getComponent(Blueprint).addToUndoStack()
                find('Canvas/Blueprint Background/Blueprint').getComponent(Blueprint).clearRedoStack()

                // 将所有工具颜色恢复原状
                this.resetAllItemColor()

                // 改变选中的工具颜色
                toolItem.getComponent(Sprite).color = new Color().fromHEX('ffffff')

                // 设置选择的工具名称
                this.selectedTool = toolItem.name
                
                // 播放音频
                this.audioSource.playOneShot(this.selectAudio)

                break
            }
        }
    }

    /** 
     * @zh
     * 重置所有工具节点的颜色。
    */
    resetAllItemColor() {
        for (let i=0; i<this.toolContentNode.children.length; i++) {
            let toolItem = this.toolContentNode.children[i]
            toolItem.getComponent(Sprite).color = new Color().fromHEX('a5a5a5')
        }
    }

    /** 
     * @zh
     * 通过工具名称选择工具，用于撤销重做功能。
    */
    selectToolByName(name: string) {
        for (let i=0; i<this.toolContentNode.children.length; i++) {
            let toolItem = this.toolContentNode.children[i]
            if (toolItem.name == name) {
                // 将所有工具颜色恢复原状
                this.resetAllItemColor()

                // 改变选中的工具颜色
                toolItem.getComponent(Sprite).color = new Color().fromHEX('ffffff')

                // 设置选择的工具名称
                this.selectedTool = toolItem.name

                break
            }
        }
    }
}